******************************************
* Time preferences and credit constraints 
* Dean & Sautmann 2019
******************************************

//A Sautmann

// ** Table 16
// this file replaces the measured MRS (TPnewA) with a random point from the identified interval
// and conducts 200 regressions to estimate the distribution of estimated coefficients.
// Note: takes a while to run.

// Change the path below to your data folder
global datafolder "/Users/Data"

version 14.2
use "$datafolder/Panel-TP.dta", clear

// Panel data set 

xtset HH week // HH is unique - one person per household
set seed 349393485
set more off

// generate random points in the interval identified by MRS experiment
forvalues no= 1/200 {
	gen TPrand`no'=.
	gen randtemp=runiform()

	forvalues i=50(50)400 {
		local j=`i'+50
		replace TPrand`no' = 300/`j'+randtemp*(300/`i'-300/`j') ///
		if TPnewA<300/`i' & TPnewA>=300/`j'
		}

	replace TPrand`no' = 6+randtemp*4 if TPnewA==8
	drop randtemp
}

reshape long TPrand, i(HH week) j(MRS_draw)

// for each household: 200 MRS_draw of TP in each week.
sort HH MRS_draw week, stable
// within HH, MRS_draw: create lag variable between weeks.
gen TPrandLag=.
replace TPrandLag=TPrand[_n-1] if week==3 | week==4

replace TPdelta = TPrand-TPrandLag


//--------------------------------------------------------------------------
// run 200 regressions each

preserve
// 1 - event spending
statsby _b _se , clear by(MRS_draw):  xtreg TPrand  EventSpend  , fe  cluster(HH)
summarize _b_*, detail

gen pEventSpend1=r(p1)
gen pEventSpend5=r(p5)
gen pEventSpend10=r(p10)
gen pEventSpend90=r(p90)
gen pEventSpend95=r(p95)
gen pEventSpend99=r(p99)

gen t=_b_E/_se_E

summarize t, detail

save "$datafolder/EventSpendRand.dta", replace

restore


preserve
// event occurrence
statsby _b _se , clear by(MRS_draw):  xtreg TPrand  Event  , fe  cluster(HH)
summarize _b_*, detail

gen pEvent1=r(p1)
gen pEvent5=r(p5)
gen pEvent10=r(p10)
gen pEvent90=r(p90)
gen pEvent95=r(p95)
gen pEvent99=r(p99)

gen t=_b_E/_se_E

summarize t, detail

save "$datafolder/EventRand.dta", replace

restore


preserve
// income
statsby _b _se , clear by(MRS_draw):  xtreg TPrand EarningsLabor EarningsNonLaborE*  , fe  cluster(HH)
summarize _b_*, detail

gen pExIncome1=r(p1)
gen pExIncome5=r(p5)
gen pExIncome10=r(p10)
gen pExIncome90=r(p90)
gen pExIncome95=r(p95)
gen pExIncome99=r(p99)

gen t=_b_EarningsNonLaborEx/_se_EarningsNonLaborEx

save "$datafolder/ExIncomeRand.dta", replace

restore

preserve
// savings, fixed effects

gen Savings2=0.5*Savings^2

statsby _b _se , clear by(MRS_draw):  xtreg TPdelta  Savings Savings2 , fe cluster(HH)
summarize _b_*, detail

gen pSavings1=r(p1)
gen pSavings5=r(p5)
gen pSavings10=r(p10)
gen pSavings90=r(p90)
gen pSavings95=r(p95)
gen pSavings99=r(p99)

gen t=_b_Savings/_se_Savings
gen t2=_b_Savings2/_se_Savings2

save "$datafolder/SavingsRand.dta", replace

restore


preserve
// savings, constant

gen Savings2=0.5*Savings^2
statsby _b _se , clear by(MRS_draw):  reg TPdelta  Savings Savings2 ,  cluster(HH)
summarize _b_*, detail

gen pSavings1=r(p1)
gen pSavings5=r(p5)
gen pSavings10=r(p10)
gen pSavings90=r(p90)
gen pSavings95=r(p95)
gen pSavings99=r(p99)

gen t=_b_Savings/_se_Savings
gen t2=_b_Savings2/_se_Savings2

save "$datafolder/SavingsRand1.dta", replace

restore

preserve
  use "$datafolder/EventRand.dta", clear
 gen prop = (t>=1.69)
 sum prop
 sum _b_E, detail
 
 use "$datafolder/EventSpendRand.dta", clear
 gen prop = (t>=1.69)
 sum prop
 sum _b_E, detail



use "$datafolder/ExIncomeRand.dta", clear
 gen prop = (t<=-1.69)
 sum prop


 sum _b_EarningsNonLaborEx, detail
 
 
use "$datafolder/SavingsRand.dta", clear
 gen prop = (t<=-1.69)
 sum prop


 sum _b_Savings, detail
 
 
use "$datafolder/SavingsRand1.dta", clear
 gen prop = (t<=-1.69)
 sum prop


 sum _b_Savings, detail

restore

erase EventRand.dta
erase EventSpendRand.dta
erase ExIncomeRand.dta
erase SavingsRand.dta
erase SavingsRand1.dta
